package com.ptu.spzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ptu.spzx.common.exception.ptuException;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.domain.Knowledge.KnowledgePoint;
import com.ptu.spzx.domain.Question.Dto.QuestionDTO;
import com.ptu.spzx.domain.Question.Vo.QuestionVO;
import com.ptu.spzx.domain.Question.po.Question;
import com.ptu.spzx.domain.Question.po.QuestionKnowledge;
import com.ptu.spzx.mapper.KnowledgePointMapper;
import com.ptu.spzx.mapper.QuestionKnowledgeMapper;
import com.ptu.spzx.mapper.QuestionMapper;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.service.IQuestionService;
import com.ptu.spzx.utils.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.plugins.jpeg.JPEGQTable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YT
 * @since 2024-01-28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionKnowledgeMapper questionKnowledgeMapper;
    @Autowired
    private KnowledgePointMapper knowledgePointMapper;

    //添加题目
    @Override
    public void addQuestion(QuestionDTO questionDTO) {
        if(questionDTO.getQuestionType()==null||questionDTO.getQuestionAnswer()==null||questionDTO.getQuestionTitle()==null||questionDTO.getKnowledge()==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        Question question = BeanCopyUtils.copyBean(questionDTO, Question.class);
        int result = questionMapper.insert(question);
        for (Long knowledgeId : questionDTO.getKnowledge()) {
            QuestionKnowledge questionKnowledge = new QuestionKnowledge();
            questionKnowledge.setQuestionId(question.getQuestionId());
            questionKnowledge.setKnowledgeId(knowledgeId);
            questionKnowledgeMapper.insert(questionKnowledge);
            //知识点的使用
            knowledgePointMapper.updateByKnowIdUse(knowledgeId);
        }
        if (result == 0) {
            throw new ptuException(ResultCodeEnum.DATA_ERROR);
        }
    }

    //根据id查题目
    @Override
    public QuestionVO queryQuestion(Long id) {
        Question question = lambdaQuery()
                .eq(Question::getQuestionId, id)
                .one();
        if (question == null) {
           throw new ptuException(ResultCodeEnum.DATA_NOT_EXIST);
        }

        QuestionVO questionVO = BeanUtil.copyProperties(question, QuestionVO.class);
//查询知识点
        HashMap<String, Object> map = new HashMap<>();
        map.put("question_id", id);

        List<QuestionKnowledge> questionKnowledges = questionKnowledgeMapper.selectByMap(map);

        List<Long> kids = questionKnowledges.stream().map(QuestionKnowledge::getKnowledgeId).collect(Collectors.toList());
//封装知识点
        questionVO.setKnowledge(kids);
        return questionVO;
    }

    //批量查询
    @Override
    public PageInfo<Question> queryQuestionList(Long courseId, Long questionType, Integer pageNum, Integer pageSize) {
//        if(pageNum!=null&& pageSize!=null){
//            //       批量查询
//            Page<Question> page = lambdaQuery()
//                    .eq(courseId != null, Question::getBelongCourseId, courseId)
//                    .eq(questionType != null, Question::getQuestionType, questionType)
//                    .page(new Page<>(pageNum, pageSize));
//
//           List<Question> questions= page.getRecords();
//            List<QuestionVO> questionVOS=new ArrayList<>();
//            HashMap<String,Object> map=new HashMap<>();
////            封装为VO集合
//            for (Question question : questions) {
//                QuestionVO questionVO = BeanUtil.copyProperties(question, QuestionVO.class);
////                查知识点集合
//                Long questionId = question.getQuestionId();
//                map.put("question_id",questionId);
//                List<QuestionKnowledge> questionKnowledges = questionKnowledgeMapper.selectByMap(map);
//                List<Long> kids = questionKnowledges.stream().map(QuestionKnowledge::getKnowledgeId).collect(Collectors.toList());
//                //封装知识点
//                questionVO.setKnowledge(kids);
//                questionVOS.add(questionVO);
//            }
//            PageInfo<QuestionVO> pageInfo=new PageInfo<>(questionVOS);
//            return pageInfo;
//        }else {
//            List<Question> list = lambdaQuery()
//                    .eq(courseId != null, Question::getBelongCourseId, courseId)
//                    .eq(questionType != null, Question::getQuestionType, questionType)
//                    .list();
//
//            List<QuestionVO> questionVOS=new ArrayList<>();
//            HashMap<String,Object> map=new HashMap<>();
////            封装为VO集合
//            for (Question question : list) {
//                QuestionVO questionVO = BeanUtil.copyProperties(question, QuestionVO.class);
////                查知识点集合
//                Long questionId = question.getQuestionId();
//                map.put("question_id",questionId);
//                List<QuestionKnowledge> questionKnowledges = questionKnowledgeMapper.selectByMap(map);
//                List<Long> kids = questionKnowledges.stream().map(QuestionKnowledge::getKnowledgeId).collect(Collectors.toList());
//                //封装知识点
//                questionVO.setKnowledge(kids);
//                questionVOS.add(questionVO);
//            }
//            PageInfo<QuestionVO> pageInfo=new PageInfo<>(questionVOS);
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(courseId==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        if (questionType != null) {
            lambdaQueryWrapper.eq(Question::getQuestionType, questionType)
                    .eq(Question::getBelongCourseId, courseId);
        }else {
            lambdaQueryWrapper.eq(Question::getBelongCourseId,courseId);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questions = questionMapper.selectList(lambdaQueryWrapper);
        PageInfo<Question> pageInfo=new PageInfo<>(questions);
        return pageInfo;
    }

    //    更新题目
    @Override
    public void updateQuestion(QuestionDTO questionDTO) {
        Long questionId = questionDTO.getQuestionId();
        Question question1 = questionMapper.selectById(questionId);
        if(question1==null){
            throw new ptuException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        Question question = BeanCopyUtils.copyBean(questionDTO, Question.class);
        questionMapper.updateById(question);
        List<Long> knowledgeIds = questionDTO.getKnowledge();
        questionKnowledgeMapper.deleteKnowledge(questionDTO.getQuestionId());
        if(knowledgeIds!=null) {
            for (Long knowledgedId : knowledgeIds) {
                QuestionKnowledge questionKnowledge = new QuestionKnowledge();
                questionKnowledge.setQuestionId(question.getQuestionId());
                questionKnowledge.setKnowledgeId(knowledgedId);
                questionKnowledgeMapper.insert(questionKnowledge);
            }
        }
    }

    //批量删除
    @Override
    public void delBatchQuestion(DeleteIds deleteIds) {
        List<Long> ids = deleteIds.getIds();
        //        判空
        if (ids == null || ids.isEmpty()) {
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }

//        删除知识点和题目
        HashMap<String, Object> map = new HashMap<>();
        for (Long id : ids) {
            map.put("question_id", id);
            removeByMap(map);
            questionKnowledgeMapper.deleteByMap(map);
        }

        return;
    }
}
